package com.example.demo.controller

import com.example.demo.dto.*
import com.example.demo.service.WebService
import com.example.demo.utils.MResult
import com.example.demo.vo.*
import com.example.demo.vo.ret.PatientInfo
import com.example.demo.vo.ret.webret.UserInfo
import com.example.demo.vo.webvo.MenuVo
import com.example.demo.vo.webvo.UserVo
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import io.swagger.annotations.ApiOperationSupport
import io.swagger.annotations.ApiSort
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile
import javax.validation.Valid


@RestController
@RequestMapping("/web")
@Api(tags = ["网页接口"])
@ApiSort(value = 2)
class WebController @Autowired constructor(
    private val mWebService: WebService
) {

    @ApiOperation(value = "登录信息")
    @PostMapping("/login")
    @ApiOperationSupport(order = 1)
    fun getLogin(@Valid loginDo: LoginDo): MResult<UserVo> {
        mWebService.getUserInfo(loginDo.username)?.let {
            if (it.password == loginDo.password) {
                return MResult<UserVo>().result(it)
            } else {
                return MResult<UserVo>().error("密码不正确")
            }
        } ?: return MResult<UserVo>().error("登录失败")
    }

    @ApiOperation(value = "菜单信息")
    @GetMapping("/menu")
    fun getMenuInfo(): MResult<List<MenuVo>> {
        mWebService.getMenuInfo()?.let {
            return MResult<List<MenuVo>>().result(it)
        } ?: return MResult<List<MenuVo>>().error("获取失败")
    }

    @ApiOperation(value = "更新用户头像")
    @PostMapping("/avatar")
    fun getUpdateAvatar(@Valid updateAvatarDo: UpdateAvatarDo): MResult<UserInfo> {
        updateAvatarDo.let {
            mWebService.getUpdateAvatar(updateAvatarDo.image, updateAvatarDo.userId)?.let {
                return MResult<UserInfo>().result(it)
            }
        } ?: return MResult<UserInfo>().error("更新失败")
    }

    @ApiOperation(value = "更新用户密码")
    @PostMapping("password/update")
    fun updatePassword(@Valid updatePasswordDo: UpdatePasswordDo): MResult<Nothing> {
        mWebService.getUserPassword(updatePasswordDo.userId)?.let {
            if (it == updatePasswordDo.oldPassword) {
                mWebService.getUpdatePassword(updatePasswordDo.userId, updatePasswordDo.newPassword)?.let {
                    return MResult<Nothing>().resultMsage("修改成功")
                }
            } else {
                return MResult<Nothing>().error("密码不正确")
            }
        } ?: return MResult<Nothing>().error("修改失败")
    }

    @ApiOperation(value = "更新用户信息")
    @PostMapping("profile/update")
    fun getUpdateUserInfo(@Valid userDo: UserDo): MResult<UserInfo> {
        mWebService.getUpdateUserInfo(userDo)?.let {
            return MResult<UserInfo>().result(it)
        } ?: return MResult<UserInfo>().error("获取失败")
    }

    @ApiOperation(value = "获取科室树")
    @PostMapping("get/deptlist")
    fun getDeptList(): MResult<List<DeptVo>> {
        mWebService.getDeptTree()?.let {
            return MResult<List<DeptVo>>().result(it)
        } ?: return MResult<List<DeptVo>>().error("获取失败")
    }

    @ApiOperation(value = "获取病人一览表信息")
    @PostMapping("patientinfolist")
    fun getPatientInfoList(@Valid patientInfoListDo: PatientInfoListDo): MResult<PatientInfo> {
        mWebService.getPatientInfoList(patientInfoListDo)?.let {
            return MResult<PatientInfo>().result(0, it)
        } ?: return MResult<PatientInfo>().error("获取失败")
    }

//    @ApiOperation(value = "获取标签")
//    @PostMapping("nursingclass")
//    fun getNursingclassList(): MResult<List<NursingclassVo>> {
//        mWebService.getNursingclassList()?.let {
//            return  MResult<List<NursingclassVo>>().result(it)
//        }?: return MResult<List<NursingclassVo>>().error("获取失败")
//    }

    @ApiOperation(value = "添加标签")
    @PostMapping("add/nursingclass")
    fun addNursingclassList(nursingClassDo: NursingClassDo): MResult<Int> {
        mWebService.addNursingclass(nursingClassDo)?.let {
            return MResult<Int>().result(it)
        } ?: return MResult<Int>().error("获取失败")
    }

    @ApiOperation(value = "删除标签")
    @PostMapping("delete/nursingclass")
    fun deleteNursingclassList(nursingClassDo: NursingClassDo): MResult<Int> {
        mWebService.deleteNursingclassList(nursingClassDo)?.let {
            return MResult<Int>().result(it)
        } ?: return MResult<Int>().error("删除失败")
    }

    @ApiOperation(value = "添加调查问卷表")
    @PostMapping("add/questionnaire")
    fun addQuestionnaire(addQuestionnaireDo: AddQuestionnaireDo): MResult<Int> {
        mWebService.addQuestionnaire(addQuestionnaireDo)?.let {
            return MResult<Int>().result(it)
        } ?: return MResult<Int>().error("添加失败")
    }

    @ApiOperation(value = "获取调查问卷表")
    @PostMapping("get/questionnaire")
    fun getListQuestionnaire(): MResult<List<QuestionnaireVo>>? {
        mWebService.getListQuestionnaire()?.let {
            return MResult<List<QuestionnaireVo>>().result(it)
        } ?: return MResult<List<QuestionnaireVo>>().error("获取失败")
    }

    @ApiOperation(value = "删除调查问卷表")
    @PostMapping("delete/questionnaire")
    fun deleteQuestionnaire(id: Int): MResult<Int> {
        mWebService.deleteQuestionnaire(id)?.let {
            return MResult<Int>().result(it)
        } ?: return MResult<Int>().error("删除失败")
    }

    @ApiOperation(value = "更新调查问卷表")
    @PostMapping("update/questionnaire")
    fun updateQuestionnaire(addQuestionnaireDo: AddQuestionnaireDo): MResult<Int> {
        mWebService.updateQuestionnaire(addQuestionnaireDo)?.let {
            return MResult<Int>().result(it)
        } ?: return MResult<Int>().error("更新失败")
    }

    @ApiOperation(value = "调查问卷表详情")
    @PostMapping("detail/questionnaire")
    fun detailQuestionnaire(id: Int): MResult<QuestionnaireInfoVo> {
        mWebService.getDetailQuestionnaire(id)?.let {
            return MResult<QuestionnaireInfoVo>().result(it)
        } ?: return MResult<QuestionnaireInfoVo>().error("获取失败")
    }

    @ApiOperation(value = "调查问卷表详情列表")
    @PostMapping("get/questionnairelist")
    fun getDetailQuestionnaireList(deptId: Int): MResult<List<QuestionnaireInfoVo>> {
        mWebService.getDetailQuestionnaireList(deptId)?.let {
            return MResult<List<QuestionnaireInfoVo>>().result(it)
        } ?: return MResult<List<QuestionnaireInfoVo>>().error("获取失败")
    }

    @ApiOperation(value = "提交调查问卷表详情列表")
    @PostMapping("post/questionnairelist")
    fun psotDetailQuestionnaireList( @RequestBody questionnaireDo: List<QuestionnaireDo>): MResult<Int> {
        mWebService.psotDetailQuestionnaireList(questionnaireDo)?.let {
            return MResult<Int>().resultMsage("提交成功")
        } ?: return MResult<Int>().error("提交失败")
    }

    @ApiOperation(value = "apk文件上传")
    @PostMapping("/upload")
    fun upload(
        @RequestParam("file") file: MultipartFile,
        apkName: String,
        apkCode: String,
        apkType: String
    ): MResult<ApkVo> {
        mWebService.addApk(file, apkName, apkCode, apkType)?.let {
            return MResult<ApkVo>().result(it)
        } ?: return MResult<ApkVo>().error("上传失败！")
    }

    @ApiOperation(value = "获取所有apk列表")
    @PostMapping("get/listapk")
    fun getListApk(): MResult<List<ApkVo>> {
        mWebService.getListApk()?.let {
            return MResult<List<ApkVo>>().result(it)
        } ?: return MResult<List<ApkVo>>().error("获取失败")
    }

    @ApiOperation(value = "删除apk")
    @PostMapping("delete/apk")
    fun deleteApk(id: Int): MResult<Int> {
        mWebService.deleteApk(id)?.let {
            return MResult<Int>().result(it)
        } ?: return MResult<Int>().error("删除失败")
    }

    @ApiOperation(value = "获取菜品类型列表")
    @PostMapping("get/foodtype")
    fun getFoodTypeList(): MResult<List<FoodTypeVo>> {
        mWebService.getListFoodType()?.let {
            return MResult<List<FoodTypeVo>>().result(it)
        } ?: return MResult<List<FoodTypeVo>>().error("获取失败")
    }

    @ApiOperation(value = "添加菜品类型")
    @PostMapping("add/foodtype")
    fun addFoodType(title: String): MResult<Int> {
        mWebService.addFoodType(title)?.let {
            return MResult<Int>().result(it)
        } ?: return MResult<Int>().error("添加失败")
    }

    @ApiOperation(value = "删除菜品类型")
    @PostMapping("delete/foodtype")
    fun deleteFoodType(id: Int): MResult<Int> {
        mWebService.deleteFoodType(id)?.let {
            return MResult<Int>().result(it)
        } ?: return MResult<Int>().error("删除失败")
    }

    @ApiOperation(value = "获取菜品列表")
    @PostMapping("get/foodlist")
    fun getFoodList(): MResult<List<FoodInfoVo>> {
        mWebService.getFoodList()?.let {
            return MResult<List<FoodInfoVo>>().result(it)
        } ?: return MResult<List<FoodInfoVo>>().error("获取失败")
    }

    @ApiOperation(value = "根据FoodTypeId获取菜品列表")
    @PostMapping("get/foodlistbyfoodTypeid")
    fun getFoodListByFoodTypeId(fooTypeId:String): MResult<List<FoodInfoVo>> {
        mWebService.getFoodListByFoodTypeId(fooTypeId.toInt())?.let {
            return MResult<List<FoodInfoVo>>().result(it)
        } ?: return MResult<List<FoodInfoVo>>().error("获取失败")
    }

    @ApiOperation(value = "添加菜品")
    @PostMapping("add/food")
    fun addFood(
        title: String,
        price: Int,
        Introduction: String,
        specification: String,
        foodTypeId: Int,
        file: MultipartFile
    ): MResult<Int> {
        mWebService.addFood(
            title, price,
            Introduction, specification, foodTypeId, file
        )?.let {
            return MResult<Int>().result(it)
        } ?: return MResult<Int>().error("添加失败")
    }

    @ApiOperation(value = "删除菜品")
    @PostMapping("delete/food")
    fun deleteFood(id: Int): MResult<Int> {
        mWebService.deleteFood(id)?.let {
            return MResult<Int>().result(it)
        } ?: return MResult<Int>().error("删除失败")
    }

    @ApiOperation(value = "更新菜品")
    @PostMapping("update/food")
    fun updateFood(
        id: Int,
        title: String,
        price: Int,
        Introduction: String,
        specification: String,
        foodTypeId: Int,
        foodTypeTitle: String
    ): MResult<Int> {
        mWebService.updateFood(id, title, price, Introduction, specification, foodTypeId, foodTypeTitle)?.let {
            return MResult<Int>().result(it)
        } ?: return MResult<Int>().error("更新失败")
    }

    @ApiOperation(value = "添加商品类型")
    @PostMapping("add/producttype")
    fun addProductType(title: String): MResult<Int> {
        mWebService.addProductType(title)?.let {
            return MResult<Int>().result(it)
        } ?: return MResult<Int>().error("添加失败")
    }

    @ApiOperation(value = "删除商品类型")
    @PostMapping("delete/producttype")
    fun deleteProductType(id: Int): MResult<Int> {
        mWebService.deleteProductType(id)?.let {
            return MResult<Int>().result(it)
        } ?: return MResult<Int>().error("删除失败")
    }


    @ApiOperation(value = "获取商品类型列表")
    @PostMapping("get/producttype")
    fun getProductTypeList(): MResult<List<ProductTypeVo>> {
        mWebService.getProductTypeList()?.let {
            return MResult<List<ProductTypeVo>>().result(it)
        } ?: return MResult<List<ProductTypeVo>>().error("添加失败")
    }

    @ApiOperation(value = "添加商品")
    @PostMapping("add/product")
    fun addProduct(
        title: String,
        price: Int,
        Introduction: String,
        specification: String,
        productTypeId: Int,
        file: MultipartFile
    ): MResult<Int> {
        mWebService.addProduct(
            title, price,
            Introduction, specification, productTypeId, file
        )?.let {
            return MResult<Int>().result(it)
        } ?: return MResult<Int>().error("添加失败")
    }


    @ApiOperation(value = "获取商品列表")
    @PostMapping("get/productlist")
    fun getProductInfoList(): MResult<List<ProductInfoVo>> {
        mWebService.getProductInfoList()?.let {
            return MResult<List<ProductInfoVo>>().result(it)
        } ?: return MResult<List<ProductInfoVo>>().error("获取失败")
    }

    @ApiOperation(value = "根据id获取商品列表")
    @PostMapping("get/productlistbyid")
    fun getProductInfoListById(productTypeId:Int): MResult<List<ProductInfoVo>> {
        mWebService.getProductInfoListById(productTypeId)?.let {
            return MResult<List<ProductInfoVo>>().result(it)
        } ?: return MResult<List<ProductInfoVo>>().error("获取失败")
    }

    @ApiOperation(value = "获取设备列表")
    @PostMapping("get/devicetlist")
    fun getDeviceList(): MResult<List<DeviceVo>> {
        mWebService.getDeviceList()?.let {
            return MResult<List<DeviceVo>>().result(it)
        } ?: return MResult<List<DeviceVo>>().error("获取失败")
    }

    @ApiOperation(value = "添加科室宣教视频")
    @PostMapping("add/deptvideo")
    fun addDeptVideo(
        deptid: Int,
        deptName: String,
        videoTitle: String,
        videoImageUrl: String,
        videoUrl: String
    ): MResult<Int> {
        mWebService.addDeptVideo(
            deptid,
            deptName,
            videoTitle,
            videoImageUrl,
            videoUrl
        )?.let {
            return MResult<Int>().result(it)
        } ?: return MResult<Int>().error("获取失败")
    }

    @ApiOperation(value = "删除科室宣教视频")
    @PostMapping("delete/deptvideo")
    fun deleteDeptVideo(id: Int): MResult<Int> {
        mWebService.deleteDeptVideo(id)?.let {
            return MResult<Int>().result(it)
        } ?: return MResult<Int>().error("删除失败")
    }

    @ApiOperation(value = "获取科室宣教视频列表")
    @PostMapping("get/deptvideolist")
    fun getDeptVideoList(deptid: Int?): MResult<List<DeptVideoVo>> {
        mWebService.getDeptVideoList(deptid)?.let {
            return MResult<List<DeptVideoVo>>().result(it)
        } ?: return MResult<List<DeptVideoVo>>().error("获取失败")
    }




}